package com.hducms.cms.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.UrlPathHelper;

import com.hducms.cms.dao.CmsLogDao;
import com.hducms.cms.dao.CmsUserDao;
import com.hducms.cms.entity.main.CmsLog;
import com.hducms.cms.entity.main.CmsUser;
import com.hducms.cms.service.CmsLogService;
import com.hducms.cms.service.CmsUserService;
import com.hducms.cms.web.CmsUtils;
import com.hducms.common.page.Pagination;
//import com.hducms.cms.dao.CmsLogDao;
//import com.hducms.cms.entity.main.CmsLog;
//import com.hducms.cms.entity.main.CmsSite;
//import com.hducms.cms.entity.main.CmsUser;
//import com.hducms.cms.manager.main.CmsLogMng;
//import com.hducms.cms.manager.main.CmsUserMng;
//import com.hducms.cms.web.CmsUtils;
//import com.hducms.common.page.Pagination;
//import com.hducms.common.web.RequestUtils;
//import com.jeecms.common.web.springmvc.MessageResolver;
import com.hducms.common.web.RequestUtils;
import com.hducms.common.web.springmvc.MessageResolver;

@Service
@Transactional
public class CmsLogServiceImpl implements CmsLogService {
	@Transactional(readOnly = true)
	public Pagination getPage(Integer category, String username, 
			String title, String ip, int pageNo, int pageSize) {
		Pagination page = new Pagination();
		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isBlank(username)) {
			CmsUser user = cmsUserMng.findByUsername(username);
			if (user != null) {
				map.put("userId", user.getUserId());
			} 
		}

		map.put("category", category);
		map.put("title", title);
		map.put("ip", ip);
		map.put("pageSize", pageSize);

		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		map.put("start", page.getFirstResult());

		int totalCount = dao.countQueryResult(map);
		page.setTotalCount(totalCount);

		if (totalCount < 1) {
			page.setList(new ArrayList());
			return page;
		}
		
		List<CmsLog> list = dao.getPage(map);
		for (CmsLog cmsLog : list) {
			if (cmsLog.getUser()==null) {
				CmsUser temp = new CmsUser();
				temp.setUsername("匿名");
				cmsLog.setUser(temp);
				temp=null;
			}
		}
		page.setList(list);		
		return page;
	}

	@Transactional(readOnly = true)
	public CmsLog findById(Integer id) {
		CmsLog entity = dao.findById(id);
		return entity;
	}

	/**
	 * 新增一条日志记录
	 * @param category  日志类型
	 * @param user 操作用户
	 * @param url URL
	 * @param ip IP
	 * @param date 日志时间
	 * @param title 日志标题
	 * @param content 日志内容
	 * @return
	 */
	public CmsLog save(Integer category, CmsUser user,
			String url, String ip, Date date, String title, String content) {
		CmsLog log = new CmsLog();
		log.setUser(user);
		log.setCategory(category);
		log.setIp(ip);
		log.setLogTime(date);
		log.setUrl(url);
		log.setTitle(title);
		log.setContent(content);
		save(log);
		return log;
	}

	public CmsLog loginSuccess(HttpServletRequest request, CmsUser user,
			String title) {
		String ip = RequestUtils.getIpAddr(request);
		UrlPathHelper helper = new UrlPathHelper();
		String uri = helper.getOriginatingRequestUri(request);
		Date date = new Date();
		String tempString = MessageResolver.getMessage(request, title);
		CmsLog log = save(CmsLog.LOGIN_SUCCESS, user, uri, ip, date,
				MessageResolver.getMessage(request, title), null);
		return log;
	}

	public CmsLog loginFailure(HttpServletRequest request, String title,
			String content) {
		String ip = RequestUtils.getIpAddr(request);
		UrlPathHelper helper = new UrlPathHelper();
		String uri = helper.getOriginatingRequestUri(request);
		Date date = new Date();
		CmsLog log = save(CmsLog.LOGIN_FAILURE, null, uri, ip, date,
				MessageResolver.getMessage(request, title), content);
		return log;
	}

	public CmsLog operating(HttpServletRequest request, String title,
			String content) {
		CmsUser user = CmsUtils.getUser(request);
		String ip = RequestUtils.getIpAddr(request);
		UrlPathHelper helper = new UrlPathHelper();
		String uri = helper.getOriginatingRequestUri(request);
		Date date = new Date();
		CmsLog log = save(CmsLog.OPERATING, user, uri, ip, date,
				MessageResolver.getMessage(request, title), content);
		return log;
	}

	public CmsLog save(CmsLog bean) {
		dao.save(bean);
		return bean;
	}

//	public int deleteBatch(Integer category, Integer siteId, Integer days) {
//		Date date = null;
//		if (days != null && days > 0) {
//			Calendar cal = Calendar.getInstance();
//			cal.add(Calendar.DAY_OF_MONTH, -days);
//			date = cal.getTime();
//		}
//		return dao.deleteBatch(category, siteId, date);
//	}

	public CmsLog deleteById(Integer id) {
		CmsLog bean = findById(id);
		dao.deleteById(id);
		return bean;
	}

	public CmsLog[] deleteByIds(Integer[] ids) {
		CmsLog[] beans = new CmsLog[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private CmsUserService cmsUserMng;
	private CmsLogDao dao;

	@Autowired
	public void setCmsUserMng(CmsUserService cmsUserMng) {
		this.cmsUserMng = cmsUserMng;
	}

	@Autowired
	public void setDao(CmsLogDao dao) {
		this.dao = dao;
	}

}